package com.wwwarehouse.contract.contract;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.contract.adapter.SelectObjAdapter;
import com.wwwarehouse.contract.bean.ReleaseObjectBean;
import com.wwwarehouse.contract.common.ContractCommon;
import com.wwwarehouse.contract.core.ContractConstant;
import com.wwwarehouse.contract.event.SelectEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/10/18.
 *         <p/>
 */

public class ReleaseObjectListFragment extends TheParentFragment {

    private RecyclerView mRvContent;
    private ArrayList<ReleaseObjectBean.BusinessListBean> tagBeanList = new ArrayList<>();
    SelectObjAdapter mAdapter;
    private int pos;
    private int numOfPage;
    private ArrayList<String> publishData;
    private String searchText;

    @Override
    protected int getContentId() {
        return R.layout.release_object_list;
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    @Override
    protected void loadDatas() {
        Bundle b = getArguments();
        if (b == null)
            return;
        pos = b.getInt("pos");
        numOfPage = b.getInt("num");
        publishData = b.getStringArrayList("publishData");
        searchText = b.getString("searchText");
        EventBus.getDefault().register(this);
        Map<String, Object> map = new HashMap<>();
        map.put("buId", b.getString("buId")); //   业务单位ID(必填)
        map.put("orderSort", b.getString("orderSort")); //  排序(DESC,ASC)
        map.put("orderType", b.getString("orderType")); //  按建立时间 related_time空为按客户名称排序
        map.put("page", pos); //  页数(分页，必填)
        map.put("relationType", b.getString("relationType")); //  CUSTOMER客户 SUPPLIER供应商
        map.put("searchText", searchText != null ? searchText : ""); // 搜索文字(选填，空则搜全部，模糊搜索业务单位名称)
        map.put("size", numOfPage); // 每页数量(分页，必填)
        //调用接口查询发布的对象
        httpPost(ContractConstant.RELATIONBUSINESSUNITLIST, map, 0, true, "");

    }

    @Override
    protected void init(View view) {
        mRvContent = findView(view, R.id.rv_content);
    }

    public void onEventMainThread(Object event) {
        if (event instanceof SelectEvent) {
            if (((SelectEvent) event).getCurrentPos() == pos) {
                if (tagBeanList.get(((SelectEvent) event).getCurrentPosition()).isSelect()){
                    if (publishData!=null&&publishData.contains(tagBeanList.get(((SelectEvent) event).getCurrentPosition()).getBuId())){
                        publishData.remove(tagBeanList.get(((SelectEvent) event).getCurrentPosition()).getBuId());
                        LogUtils.showLog("remove+--->"+tagBeanList.get(((SelectEvent) event).getCurrentPosition()).getBuId());
                    }
                }  else {
                    if (publishData!=null&&!publishData.contains(tagBeanList.get(((SelectEvent) event).getCurrentPosition()).getBuId())){
                        publishData.add(tagBeanList.get(((SelectEvent) event).getCurrentPosition()).getBuId());
                        LogUtils.showLog("add+--->"+tagBeanList.get(((SelectEvent) event).getCurrentPosition()).getBuId());
                    } else if (publishData==null){
                        publishData = new ArrayList<>();
                        publishData.add(tagBeanList.get(((SelectEvent) event).getCurrentPosition()).getBuId());
                    }
                }
                ContractCommon.getInstance().setPublishObjData(publishData);
                tagBeanList.get(((SelectEvent) event).getCurrentPosition()).setSelect(!tagBeanList.get(((SelectEvent) event).getCurrentPosition()).isSelect());
                mAdapter.notifyItemChanged(((SelectEvent) event).getCurrentPosition());
            }
        }

    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        if (what == 0) {
            if (TextUtils.equals("0", commonClass.getCode())) {
                ReleaseObjectBean releaseObjectBean = JSON.parseObject(commonClass.getData().toString(), ReleaseObjectBean.class);
                if (releaseObjectBean.getBusinessList() != null) {
                    tagBeanList.addAll(releaseObjectBean.getBusinessList());
                }

                if (publishData != null && tagBeanList != null) {
                    for (int i = 0; i < tagBeanList.size(); i++) {
                        for (int j = 0; j < publishData.size(); j++) {
                            if (publishData.get(j).equals(tagBeanList.get(i).getBuId())) {
                                tagBeanList.get(i).setSelect(true);
                            }
                        }
                    }
                }


                mRvContent.setLayoutManager(new GridLayoutManager(mActivity, 1) {
                    @Override
                    public boolean canScrollHorizontally() {
                        return false;
                    }

                    @Override
                    public boolean canScrollVertically() {
                        return false;
                    }

                });
                final ViewTreeObserver observer = mRvContent.getViewTreeObserver();
                observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        observer.removeGlobalOnLayoutListener(this);
                        if (mAdapter == null) {
                            mAdapter = new SelectObjAdapter(mActivity, tagBeanList, mRvContent.getHeight(), mRvContent.getWidth(), numOfPage, pos);
                        }
                        mRvContent.setAdapter(mAdapter);
                    }
                });

            } else {
                toast(commonClass.getMsg());
            }
        }


    }

    @Override
    public void onReLoad(int code) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
